#include "UserInfo.h"

UserInfoContainer::UserInfoContainer()
{
	
}

UserInfoContainer::~UserInfoContainer()
{
	UserInfo *pUserInfo = NULL;	

	SYNC_POINTER(this);
	
	for(IterUser iter = m_MapUser.begin(); iter != m_MapUser.end(); ++iter)
	{
		pUserInfo = iter->second;
		pUserInfo->Release();
	}

	m_MapUser.clear();
}

bool UserInfoContainer::InsertUser(DWORD dwUserSN, UserInfo *pUserInfo)
{
	SYNC_POINTER(this);

	std::pair<IterUser, bool> result = m_MapUser.insert(std::map<DWORD, UserInfo*>::value_type(dwUserSN, pUserInfo));
	if(result.second)
	{
		pUserInfo->AddRef();
		return true;
	}
	else
	{
		return false;
	}	
}

UserInfo *UserInfoContainer::FindUser(DWORD dwUserSN)
{
	UserInfo *pUserInfo = NULL;

	SYNC_POINTER(this);

	IterUser iter = m_MapUser.find(dwUserSN);
	if(iter != m_MapUser.end())
	{
		pUserInfo = iter->second;
		pUserInfo->AddRef();
	}

	return pUserInfo;
}

bool UserInfoContainer::DeleteUser(DWORD dwUserSN)
{
	SYNC_POINTER(this);

	IterUser iter = m_MapUser.find(dwUserSN);
	if(iter != m_MapUser.end())
	{
		UserInfo *pUserInfo = iter->second;
		pUserInfo->Release();

		m_MapUser.erase(iter);	

		return true;
	}

	return false;
}